package com.fingard.dsp.bank.directbank.ccblifeicbc02;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import com.fingard.constant.Format;
import com.fingard.diagnostics.LogHelper;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingleBat;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingleBat;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingleBat;
import com.fingard.dsp.bank.reqrespobj.single.detail.ReqQrySglBatPayDetail;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlReadMode;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 单笔支付
 * 
 * 需求描述：
 * 		1、指令代码：PAYENT -- 支持单笔支付及批量支付
 * 		2、指令代码：QPAYENT -- 支持单笔支付及批量支付查询
 * @author Cruise
 * @date 2018/10/23
 */
public class CCBLifeICBC02SinglePay extends CCBLifeICBC02Base implements ISinglePay {
	public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
		RetAtsSglDetail tmpRetDetail = d_190802(p_req1908);
		if ("-1".equals(tmpRetDetail.respCode) && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)) {
			tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
		}
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	private String pickupPayInfoCode(String p_msg, String p_cmpCode) {
		int tmpIndex1 = p_msg.indexOf('[');
		if (tmpIndex1 >= 0) {
			int tmpIndex2 = p_msg.indexOf(']', tmpIndex1);
			if (tmpIndex2 > tmpIndex1 + 1) {
				tmpIndex1 = tmpIndex1 + 1;
				String tmpMsgCode = p_msg.substring(tmpIndex1, tmpIndex2).trim();
				if (tmpMsgCode.startsWith("9301") || tmpMsgCode.contains(p_cmpCode.trim())) {
					return tmpMsgCode;
				}
			}
		}
		return "";
	}

	private RetAtsSglDetail d_190802(ReqSingle p_req1908) throws Exception {
		RetAtsSglDetail retDetail = new RetAtsSglDetail();
		
		CCBLifeICBC02Header tmpBankHeader = new CCBLifeICBC02Header();
		tmpBankHeader.packageID = p_req1908.singleDetail.askNumber;
		tmpBankHeader.fSeqNo = p_req1908.singleDetail.askNumber;
		tmpBankHeader.transCode = "PAYENT";
		XmlTextWriter xmlWriter = new XmlTextWriter();
		writeCommonHeader(xmlWriter, tmpBankHeader);
		xmlWriter.writeElementString("OnlBatF", "1");
		xmlWriter.writeElementString("SettleMode", "0");
		xmlWriter.writeElementString("TotalNum", "1");
		String tmpPayAmt = StringHelper.amountMultiply100(p_req1908.singleDetail.payAmount);
		xmlWriter.writeElementString("TotalAmt", tmpPayAmt);

		Date tmpDateNow = new Date();
		String dateStr = Format.DateTime17Format.format(tmpDateNow);
		String testDate = getBankConfig().getValueAsString("testDate");
		if (testDate != null && !"".equals(testDate)) {
			dateStr = testDate + dateStr.substring(8, 17);
			String passHour = getBankConfig().getValueAsString("passHours");
			String passMiniue = getBankConfig().getValueAsString("passMiniue");
			if (passHour != null && !"".equals(passHour)) {
//					yyyyMMddHHmmssSSS
				Calendar cal = Calendar.getInstance();
				cal.setTime(Format.DateTime17Format.parse(dateStr));
				cal.add(Calendar.HOUR_OF_DAY, Integer.parseInt(passHour));
				cal.add(Calendar.MINUTE,Integer.parseInt(passMiniue));
				dateStr = Format.DateTime17Format.format(cal.getTime());
			}
		}

		xmlWriter.writeElementString("SignTime", dateStr);

		// xmlWriter.writeElementString("SignTime",
		// Format.DateTime17Format.format(tmpDateNow));
		xmlWriter.writeElementString("ReqReserved1", p_req1908.singleDetail.abstractATS);
		xmlWriter.writeElementClosed("ReqReserved2");
		xmlWriter.writeElementString("AlertFlag", "0");
		
		XElement tmpXElement = convertPayRecDetailToBankXElement(p_req1908.singleDetail, 1, tmpBankHeader);
		xmlWriter.writeXElement(tmpXElement);
		xmlWriter.writeEndDocument();

		String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

		if (tmpStrRet[0].length() == 0) {
			StringBuilder tmpSbMsg = new StringBuilder();
			XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
			if (tmpBkReader != null) {
				String tmpRetCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
				String tmpRetMsg = tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");
				if (!StringHelper.isNullOrEmpty(tmpRetCode)) {
					retDetail.respCode = "0";
					retDetail.respInfo = tmpRetMsg;
					retDetail.reqSeqID = tmpBkReader.getFirstText("/CMS/eb/pub/fSeqno");
					if (tmpBkReader.read("/CMS/eb/out/rd")) {
						retDetail.transState = tmpRetCode + "+" + tmpBkReader.readedXElement.getSingleText("Result");
						if (!StringHelper.isNullOrEmpty(tmpBkReader.readedXElement.getSingleText("iRetCode"))) {
							retDetail.payInfoCode = tmpBkReader.readedXElement.getSingleText("iRetCode");
						}
						if (!StringHelper.isNullOrEmpty(tmpBkReader.readedXElement.getSingleText("iRetMsg"))) {
							retDetail.respInfo = tmpBkReader.readedXElement.getSingleText("iRetMsg");
							String iRetMsg = tmpBkReader.readedXElement.getSingleText("iRetMsg");
							String pkInfoCode = this.pickupPayInfoCode(iRetMsg, retDetail.payInfoCode);
							if (StringHelper.hasAnyChar(pkInfoCode)) {
								retDetail.payInfoCode = pkInfoCode;
							}
						}
					} else {
						retDetail.transState = tmpRetCode;
						retDetail.payInfoCode = tmpRetCode;
					}
					if (!"0".equals(tmpRetCode) && StringHelper.isNullOrEmpty(retDetail.respInfo)) {
						retDetail.respInfo = tmpRetMsg;
					}
				} else {
					retDetail.respCode = "-1";
					retDetail.respInfo = "/CMS/eb/pub/RetCode节点为空";
				}
			} else {
				retDetail.respCode = "-1";
				retDetail.respInfo = tmpSbMsg.toString();
			}
		} else if (ConnState.FAIL.equals(tmpStrRet[0])) {
			retDetail.respCode = "0";
			retDetail.respInfo = tmpStrRet[1];
			retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
			retDetail.transState = PaymentState.FAIL_Payment;
			retDetail.payInfoCode = tmpStrRet[0];
		} else {
			retDetail.respCode = "-1";
			retDetail.respInfo = tmpStrRet[1];
		}
		return retDetail;
	}

	public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
		RetAtsSglDetail tmpRetAtsDetail = new RetAtsSglDetail();

		try {
			CCBLifeICBC02Header tmpBankHeader = new CCBLifeICBC02Header();
			tmpBankHeader.transCode = "QPAYENT";
			tmpBankHeader.fSeqNo = p_req1944.reqHeader.transSeqID;
			XmlTextWriter xmlWriter = new XmlTextWriter();
			writeCommonHeader(xmlWriter, tmpBankHeader);
			xmlWriter.writeElementString("QryfSeqno", p_req1944.askNumber);
			xmlWriter.writeElementClosed("QrySerialNo");
			xmlWriter.writeEndDocument();

			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

			if (tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					String tmpRetCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
					String tmpRetMsg = tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");
					if (!StringHelper.isNullOrEmpty(tmpRetCode)) {
						tmpRetAtsDetail.reqSeqID = tmpBkReader.getFirstText("/CMS/eb/out/QryfSeqno");
						if (tmpBkReader.read("/CMS/eb/out/rd")) {
							tmpRetAtsDetail.respCode = "0";
							String instrRetMsg = tmpBkReader.readedXElement.getSingleText("instrRetMsg");
							String iRetMsg = tmpBkReader.readedXElement.getSingleText("iRetMsg");
							if (!StringHelper.isNullOrEmpty(instrRetMsg)) {
								tmpRetAtsDetail.respInfo = instrRetMsg;
							} else if (!StringHelper.isNullOrEmpty(iRetMsg)) {
								tmpRetAtsDetail.respInfo = iRetMsg;
							} else {
								tmpRetAtsDetail.respInfo = tmpRetMsg;
							}
							String instrRetCode = tmpBkReader.readedXElement.getSingleText("instrRetCode");
							String iRetCode = tmpBkReader.readedXElement.getSingleText("iRetCode");
							if (!StringHelper.isNullOrEmpty(instrRetCode)) {
								tmpRetAtsDetail.payInfoCode = instrRetCode;
							} else if (!StringHelper.isNullOrEmpty(iRetCode)) {
								tmpRetAtsDetail.payInfoCode = iRetCode;
							} else {
								tmpRetAtsDetail.payInfoCode = tmpRetCode;
							}

							String pkInfoCode = this.pickupPayInfoCode(tmpRetAtsDetail.respInfo,
									tmpRetAtsDetail.payInfoCode);
							if (StringHelper.hasAnyChar(pkInfoCode)) {
								tmpRetAtsDetail.payInfoCode = pkInfoCode;
							} else if (!StringHelper.isNullOrEmpty(iRetMsg)) {
								pkInfoCode = this.pickupPayInfoCode(iRetMsg, tmpRetAtsDetail.payInfoCode);
								if (StringHelper.hasAnyChar(pkInfoCode)) {
									tmpRetAtsDetail.payInfoCode = pkInfoCode;
								}
							}

							String tmpResult = "";
							if (tmpBkReader.readedXElement.getSingleText("Result") != null) {
								tmpResult = tmpBkReader.readedXElement.getSingleText("Result");
							}
							if ("0".equals(tmpRetCode) && tmpResult.length() == 0) {
								if (!StringHelper.isNullOrEmpty(instrRetCode)) {
									tmpResult = instrRetCode;
								}
							}
							tmpRetAtsDetail.transState = tmpRetCode + "+" + tmpResult;// 返回银行状态码
						} else if (tmpRetCode.contains("B0116")) {
							tmpRetAtsDetail.respCode = "0";
							tmpRetAtsDetail.respInfo = tmpRetMsg;
							String tmpResult = "";
							tmpBkReader.resetReadMode(XmlReadMode.SimpleInnerText);
							if (tmpBkReader.getSimpleText("Result") != null) {
								tmpResult = tmpBkReader.getSimpleText("Result");
							}
							tmpRetAtsDetail.payInfoCode = tmpRetCode;
							if (StringHelper.isNullOrEmpty(tmpRetAtsDetail.reqSeqID)) {
								tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
							}
							if (canBeNotExist(p_req1944, tmpRetAtsDetail)) {
								tmpRetAtsDetail.transState = tmpRetCode + "+" + tmpResult;
							}

						} else {
							tmpRetAtsDetail.respCode = "-1";
							tmpRetAtsDetail.respInfo = tmpRetMsg;
						}
					} else {
						tmpRetAtsDetail.respCode = "-1";
						tmpRetAtsDetail.respInfo = "节点/CMS/eb/pub/RetCode为空";
					}
				} else {
					tmpRetAtsDetail.respCode = "-1";
					tmpRetAtsDetail.respInfo = tmpSbMsg.toString();
				}
			} else {
				tmpRetAtsDetail.respCode = "-1";
				tmpRetAtsDetail.respInfo = tmpStrRet[1];
			}
		} catch (Exception e) {
			tmpRetAtsDetail.respCode = "-1";
			tmpRetAtsDetail.respInfo = e.getMessage();
			e.printStackTrace();
		}

		retAts.writeRetDetail(tmpRetAtsDetail);
		return retAts;
	}

	public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
		RetAtsSingleBat retAts = new RetAtsSingleBat(p_req1908BAT);

		XmlTextWriter xmlWriter = new XmlTextWriter();
		xmlWriter.autoFormat = false;

		CCBLifeICBC02Header tmpBankHeader = new CCBLifeICBC02Header();
		tmpBankHeader.packageID = p_req1908BAT.reqBatchNO;
		tmpBankHeader.fSeqNo = p_req1908BAT.reqBatchNO;
		tmpBankHeader.transCode = "PAYENT";

		try {
			writeBatCommonHeader(xmlWriter, tmpBankHeader);
			xmlWriter.writeElementString("OnlBatF", "1");
			xmlWriter.writeElementString("SettleMode", "0");
			xmlWriter.writeElementString("TotalNum", p_req1908BAT.totalNum);
			xmlWriter.writeElementString("TotalAmt", StringHelper.amountMultiply100(p_req1908BAT.totalAmt));

			Date tmpDateNow = new Date();
			String dateStr = Format.DateTime17Format.format(tmpDateNow);
			String testDate = getBankConfig().getValueAsString("testDate");
			if (testDate != null && !"".equals(testDate)) {
				dateStr = testDate + dateStr.substring(8, 17);
				String passHour = getBankConfig().getValueAsString("passHours");
				String passMiniue = getBankConfig().getValueAsString("passMiniue");
				if (passHour != null && !"".equals(passHour)) {
//					yyyyMMddHHmmssSSS
					Calendar cal = Calendar.getInstance();
					cal.setTime(Format.DateTime17Format.parse(dateStr));
					cal.add(Calendar.HOUR_OF_DAY, Integer.parseInt(passHour));
					cal.add(Calendar.MINUTE,Integer.parseInt(passMiniue));
					dateStr = Format.DateTime17Format.format(cal.getTime());
				}
			}

			xmlWriter.writeElementString("SignTime", dateStr);
			xmlWriter.writeElementClosed("ReqReserved1");
			xmlWriter.writeElementClosed("ReqReserved2");

			int i = 1;
			while (p_req1908BAT.readRecord()) {
				XElement tmpXElement = convertPayRecDetailToBankXElement(p_req1908BAT.readedDetail, i, tmpBankHeader);

				xmlWriter.writeXElement(tmpXElement);

				i++;
			}

			xmlWriter.writeEndDocument();

			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
			if (tmpStrRet[0].length() == 0) {
				if (tmpStrRet[1].startsWith("errorCode")) {
					retAts.batRespCode = "-1";
					retAts.batRespCode = tmpStrRet[1];
					retAts.batTransState = tmpStrRet[1];
					retAts.writeAfterDetail();
					return retAts;
				} else {

					StringBuilder tmpSbMsg = new StringBuilder();
					XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
					if (tmpBkReader != null) {
						String returnCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
						String returnMsg = tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");
						if (StringHelper.isNullOrEmpty(returnMsg)) {
							returnMsg = tmpBkReader.getFirstText("/CMS/eb/pub/SerialNo");
						}
						retAts.batRespCode = "0";
						retAts.batRespInfo = returnMsg;
						retAts.batTransState = returnCode;
						retAts.batPayInfoCode = returnCode;
						String fSeqno = tmpBkReader.getFirstText("/CMS/eb/pub/fSeqno");
						if (fSeqno == null || !fSeqno.equals(p_req1908BAT.reqBatchNO)) {
							retAts.batRespCode = "-1";
							retAts.batRespInfo = "响应支付批次号" + fSeqno + "与请求" + p_req1908BAT.reqBatchNO + "不相等";
						} else {
							while (tmpBkReader.read("/CMS/eb/out/rd")) {
								RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
								XElement tmpRetNode = tmpBkReader.readedXElement;

								tmpRetDetail.respCode = "0";
								String iRetCode = tmpRetNode.getSingleTextNotNull("iRetCode");
								String iRetMsg = tmpRetNode.getSingleTextNotNull("iRetMsg");
								tmpRetDetail.respInfo = iRetMsg;
								tmpRetDetail.payInfoCode = iRetCode;
								String pkInfoCode = this.pickupPayInfoCode(iRetMsg, iRetCode);
								if (StringHelper.hasAnyChar(pkInfoCode)) {
									tmpRetDetail.payInfoCode = pkInfoCode;
								}
								tmpRetDetail.reqSeqID = tmpRetNode.getSingleText("ERPSqn");
								tmpRetDetail.transState = StringHelper.joinNotEmpty("+", returnCode,
										tmpRetNode.getSingleText("Result"));

								retAts.writeRetDetail(tmpRetDetail);
							}
						}
					} else {
						retAts.batRespCode = "-1";
						retAts.batRespInfo = tmpSbMsg.toString();
					}
				}
			} else if (ConnState.FAIL.equals(tmpStrRet[0])) {
				retAts.batRespCode = "0";
				retAts.batRespInfo = tmpStrRet[1];
				retAts.batTransState = PaymentState.FAIL_Payment;
				retAts.batPayInfoCode = tmpStrRet[0];
				retAts.batBizState = tmpStrRet[0];
			} else {
				retAts.batRespCode = "-1";
				retAts.batRespInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();

			retAts.batRespCode = "-1";
			retAts.batRespInfo = "发生异常：" + LogHelper.getStackTraceFirstLine(ex) + "，详情请见文件日志";

			WriteBankLogLn(ex);
		}

		retAts.writeAfterDetail();
		return retAts;
	}

	public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
		RetAtsSingleBat retAts = new RetAtsSingleBat(p_req1944BAT.reqHeader);
		retAts.reqBatchNO = p_req1944BAT.reqBatchNO;

		XmlTextWriter xmlWriter = new XmlTextWriter();
		xmlWriter.autoFormat = false;

		CCBLifeICBC02Header tmpBankHeader = new CCBLifeICBC02Header();
		tmpBankHeader.fSeqNo = p_req1944BAT.reqHeader.transSeqID;
		tmpBankHeader.transCode = "QPAYENT";

		try {
			writeBatCommonHeader(xmlWriter, tmpBankHeader);
			xmlWriter.writeElementString("QryfSeqno", p_req1944BAT.reqBatchNO);
			xmlWriter.writeElementClosed("QrySerialNo");
			xmlWriter.writeElementClosed("ReqReserved1");
			xmlWriter.writeElementClosed("ReqReserved2");

			xmlWriter.writeEndDocument();

			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
			if (tmpStrRet[0].length() == 0) {
				if (tmpStrRet[1].startsWith("errorCode")) {
					retAts.batRespCode = "-1";
					retAts.batRespInfo = tmpStrRet[1];
					retAts.writeAfterDetail();
					return retAts;
				} else {
					StringBuilder tmpSbMsg = new StringBuilder();
					XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
					if (tmpBkReader != null) {

						HashMap<String, ReqQrySglBatPayDetail> mapQryDetail = p_req1944BAT.readAllDetail();

						String returnCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
						String returnMsg = tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");
						retAts.batRespCode = "-1";
						retAts.batRespInfo = returnMsg;
						if (StringHelper.hasAnyChar(returnCode)) {
							if ("0".equals(returnCode) || "B0116".equals(returnCode) || "00000".equals(returnCode)
									|| "B0097".equals(returnCode) || "B0067".equals(returnCode)
									|| "B0041".equals(returnCode)) {
								String tmpQryfSeqno = tmpBkReader.getFirstText("/CMS/eb/out/QryfSeqno");
								if (tmpQryfSeqno != null && tmpQryfSeqno.equals(retAts.reqBatchNO)) {
									retAts.batRespCode = "0";
									retAts.batTransState = returnCode;
									retAts.batPayInfoCode = returnCode;
									int rdCount = 0;
									while (tmpBkReader.read("/CMS/eb/out/rd")) {
										rdCount++;
										RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
										XElement tmpRetNode = tmpBkReader.readedXElement;

										tmpRetDetail.respCode = "0";

										if (tmpRetNode.getSingleTextNotNull("instrRetMsg").length() > 0) {
											tmpRetDetail.respInfo = tmpRetNode.getSingleText("instrRetMsg");
										}
										String iRetMsg = tmpRetNode.getSingleText("iRetMsg");
										if (StringHelper.isNullOrEmpty(tmpRetDetail.respInfo)) {
											tmpRetDetail.respInfo = tmpRetNode.getSingleText("iRetMsg");
										}

										String result = tmpRetNode.getSingleText("Result");
										if ("0".equals(returnCode) && StringHelper.isNullOrEmpty(result)) {
											result = tmpRetNode.getSingleText("instrRetCode");
										}

										String payInfoCode = tmpRetNode.getSingleText("instrRetCode");
										if (StringHelper.isNullOrEmpty(payInfoCode)) {
											payInfoCode = tmpRetNode.getSingleText("iRetCode");
										}
										tmpRetDetail.reqSeqID = tmpRetNode.getSingleText("ERPSqn");

										tmpRetDetail.transState = returnCode + "+" + result;
										tmpRetDetail.payInfoCode = payInfoCode;

										String pkInfoCode = this.pickupPayInfoCode(tmpRetDetail.respInfo,
												tmpRetDetail.payInfoCode);
										if (StringHelper.hasAnyChar(pkInfoCode)) {
											tmpRetDetail.payInfoCode = pkInfoCode;
										} else if (!StringHelper.isNullOrEmpty(iRetMsg)) {
											pkInfoCode = this.pickupPayInfoCode(iRetMsg, tmpRetDetail.payInfoCode);
											if (StringHelper.hasAnyChar(pkInfoCode)) {
												tmpRetDetail.payInfoCode = pkInfoCode;
											}
										}

										tmpRetDetail.transAmt = StringHelper
												.amountDivide100(tmpRetNode.getSingleText("PayAmt"));
										if (mapQryDetail.containsKey(tmpRetDetail.reqSeqID)) {
											if (tmpRetDetail.transState.contains("B0116")) {
												ReqQrySingle tmpReq1944 = new ReqQrySingle(
														mapQryDetail.get(tmpRetDetail.reqSeqID),
														p_req1944BAT.reqHeader);
												if (canBeNotExist(tmpReq1944, tmpRetDetail)) {
													tmpRetDetail.respCode = "0";
												}
											}

											retAts.writeRetDetail(tmpRetDetail);
										}
									}
									if (retAts.batTransState.contains("B0116") && rdCount == 0) {// 整批失败
										retAts.batRespCode = "-1";
										RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
										String firstKey = mapQryDetail.keySet().iterator().next();
										ReqQrySingle tmpReq1944 = new ReqQrySingle(mapQryDetail.get(firstKey),
												p_req1944BAT.reqHeader);
										if (canBeNotExist(tmpReq1944, tmpRetDetail)) {
											retAts.batRespCode = "0";
											tmpRetDetail.respCode = "0";
											tmpRetDetail.reqSeqID = firstKey;
											tmpRetDetail.transState = retAts.batTransState;
											tmpRetDetail.payInfoCode = retAts.batPayInfoCode;
											tmpRetDetail.respInfo = retAts.batRespInfo;
										} else {
											retAts.batRespInfo = tmpRetDetail.respInfo;
										}
									}
								} else {
									retAts.batRespCode = "-1";
									retAts.batRespInfo = "请求QryfSeqno值" + retAts.reqBatchNO + "与响应值" + tmpQryfSeqno
											+ "不相等";
								}
							} else {
								retAts.batRespCode = "-1";
							}
						}
					} else {
						retAts.batRespCode = "-1";
						retAts.batRespInfo = tmpSbMsg.toString();
					}
				}
			} else {
				retAts.batRespCode = "-1";
				retAts.batRespInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.batRespCode = "-1";
			retAts.batRespInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		retAts.writeAfterDetail();
		return retAts;
	}
}
